package com.cloudkinto.controller.stock;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.dto.stock.snap.sku.StockSkuSegmentPageDto;
import com.cloudkinto.dto.stock.snap.sku.StockSkuSnapPageDto;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.stock.StockHistoryDetailService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author hua
 * @since 2024-12-03
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "OMS库存快照模块")
@RequestMapping("/api/admin/oms/StockHistoryDetail")
public class StockHistoryDetailOmsController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(StockHistoryDetailOmsController.class);

    private final StockHistoryDetailService service;


    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit(getTenantId(),getCompanyId());
    }

    @UserSessionRequired
    @GetMapping("/productStockAgeExport")
    @ApiOperation(value = "标准产品库龄导出", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id 多个,隔开  sku___whId___stockAttribute___date 组成"),
            @ApiImplicitParam(name = "storageIds", value = "仓库 多个,隔开"),
            @ApiImplicitParam(name = "sku", value = "sku模糊搜索"),
            @ApiImplicitParam(name = "productName", value = "产品名称"),
            @ApiImplicitParam(name = "stockAttribute", value = "库存属性"),
            @ApiImplicitParam(name = "dateColumn", value = "日期类型"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "sort", value = "排序方式"),
            @ApiImplicitParam(name = "sortColumn", value = "排序列"),
    })
    public void productStockAgeExport(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = this.getProductMap(request);
        String ids = request.getParameter("ids");
        if (ids != null) {
            map.put("ids", Arrays.asList(ids.split(",")));
        }
        service.productStockAgeExport(map, response);
    }




    @UserSessionRequired
    @GetMapping("/productStockAge")
    @ApiOperation(value = "标准产品库龄（分页）", notes = "标准产品库龄（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storageIds", value = "仓库 多个,隔开"),
            @ApiImplicitParam(name = "sku", value = "sku模糊搜索"),
            @ApiImplicitParam(name = "spu", value = "spu模糊搜索"),
            @ApiImplicitParam(name = "spuAccurate", value = "spu精准搜索"),
            @ApiImplicitParam(name = "productName", value = "产品名称"),
            @ApiImplicitParam(name = "stockAttribute", value = "库存属性"),
            @ApiImplicitParam(name = "dateColumn", value = "日期类型"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "sort", value = "排序方式"),
            @ApiImplicitParam(name = "sortColumn", value = "排序列"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = StockSkuSnapPageDto.class))
    public PageResult productStockAge(HttpServletRequest request){
        Map<String, Object> map = this.getProductMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.productStockAge(map);
    }

    public Map<String, Object> getProductMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String sku = request.getParameter("sku");
        String productName = request.getParameter("productName");
        String sort = request.getParameter("sort");
        String sortColumn = request.getParameter("sortColumn");
        String[] storageIds = request.getParameterValues("storageIds");
        map.put("sku", sku);
        map.put("productName", productName);
        map.put("sort", sort);
        map.put("sortColumn", sortColumn);
        if (storageIds != null && storageIds.length > 0) {
            map.put("storageIds", Arrays.asList(storageIds));
        }
        map.put("companyId", getCompanyId());
        map.put("tenantId", getTenantId());
        String stockAttribute = request.getParameter("stockAttribute");
        map.put("stockAttribute", stockAttribute);
        String dateColumn = request.getParameter("dateColumn");
        String beginTime = request.getParameter("beginTime");
        String endTime = request.getParameter("endTime");
        map.put("dateColumn", dateColumn);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("type", 1);

        map.put("spuAccurate", request.getParameter("spuAccurate"));
        map.put("spu", request.getParameter("spu"));
        return map;
    }


    @UserSessionRequired
    @GetMapping("/boxStockAgeExport")
    @ApiOperation(value = "标准箱库龄导出", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id 多个,隔开  sku___whId___stockAttribute___date 组成"),
            @ApiImplicitParam(name = "storageIds", value = "仓库 多个,隔开"),
            @ApiImplicitParam(name = "sku", value = "箱类型号"),
            @ApiImplicitParam(name = "productName", value = "自定义箱条码"),
            @ApiImplicitParam(name = "inBoxSku", value = "SKU"),
            @ApiImplicitParam(name = "dateColumn", value = "日期类型"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "sort", value = "排序方式"),
            @ApiImplicitParam(name = "sortColumn", value = "排序列"),
    })
    public void boxStockAgeExport(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = this.getBoxMap(request);
        String ids = request.getParameter("ids");
        if (ids != null) {
            map.put("ids", Arrays.asList(ids.split(",")));
        }
        service.boxStockAgeExport(map, response);
    }


    @UserSessionRequired
    @GetMapping("/boxStockAge")
    @ApiOperation(value = "标准箱库龄（分页）", notes = "标准箱库龄（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storageIds", value = "仓库 多个,隔开"),
            @ApiImplicitParam(name = "sku", value = "箱类型号"),
            @ApiImplicitParam(name = "productName", value = "自定义箱条码"),
            @ApiImplicitParam(name = "inBoxSku", value = "SKU"),
            @ApiImplicitParam(name = "dateColumn", value = "日期类型"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "sort", value = "排序方式"),
            @ApiImplicitParam(name = "sortColumn", value = "排序列"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = StockSkuSnapPageDto.class))
    public PageResult boxStockAge(HttpServletRequest request){

        Map<String, Object> map = this.getBoxMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.productStockAge(map);
    }

    public Map<String, Object> getBoxMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();

        String sku = request.getParameter("sku");
        String productName = request.getParameter("productName");
        String sort = request.getParameter("sort");
        String sortColumn = request.getParameter("sortColumn");
        String[] storageIds = request.getParameterValues("storageIds");
        map.put("sku", sku);
        map.put("productName", productName);
        map.put("sort", sort);
        map.put("sortColumn", sortColumn);
        if (storageIds != null && storageIds.length > 0) {
            map.put("storageIds", Arrays.asList(storageIds));
        }
        map.put("companyId", getCompanyId());
        map.put("tenantId", getTenantId());
        String customizeBarcode = request.getParameter("customizeBarcode");
        map.put("customizeBarcode", customizeBarcode);
        String inBoxSku = request.getParameter("inBoxSku");
        map.put("inBoxSku", inBoxSku);

        String dateColumn = request.getParameter("dateColumn");
        String beginTime = request.getParameter("beginTime");
        String endTime = request.getParameter("endTime");
        map.put("dateColumn", dateColumn);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("type", 2);
        return map;
    }


    @UserSessionRequired
    @GetMapping("/productSegAge")
    @ApiOperation(value = "分段产品库龄（分页）", notes = "分段产品库龄（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storageIds", value = "仓库 多个,隔开"),
            @ApiImplicitParam(name = "sku", value = "sku模糊搜索"),
            @ApiImplicitParam(name = "productName", value = "产品名称"),
            @ApiImplicitParam(name = "stockAttribute", value = "库存属性"),
            @ApiImplicitParam(name = "date", value = "开始时间"),
            @ApiImplicitParam(name = "sort", value = "排序方式"),
            @ApiImplicitParam(name = "sortColumn", value = "排序列"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = StockSkuSegmentPageDto.class))
    public PageResult productSegAge(HttpServletRequest request){

        Map<String, Object> map = this.getProductSegMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.productSegAge(map);
    }


    public Map<String, Object> getProductSegMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        String date = request.getParameter("date");
        map.put("date", date);

        String sku = request.getParameter("sku");
        String productName = request.getParameter("productName");
        String sort = request.getParameter("sort");
        String sortColumn = request.getParameter("sortColumn");
        String[] storageIds = request.getParameterValues("storageIds");
        map.put("sku", sku);
        map.put("productName", productName);
        map.put("sort", sort);
        map.put("sortColumn", sortColumn);
        if (storageIds != null && storageIds.length > 0) {
            map.put("storageIds", Arrays.asList(storageIds));
        }
        String stockAttribute = request.getParameter("stockAttribute");
        map.put("stockAttribute", stockAttribute);
        map.put("companyId", getCompanyId());
        map.put("tenantId", getTenantId());
        map.put("type", 1);
        return map;
    }

    @UserSessionRequired
    @GetMapping("/productSegAgeExport")
    @ApiOperation(value = "分段产品库龄导出", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id 多个,隔开  sku___whId___stockAttribute___date 组成"),
            @ApiImplicitParam(name = "storageIds", value = "仓库 多个,隔开"),
            @ApiImplicitParam(name = "sku", value = "sku模糊搜索"),
            @ApiImplicitParam(name = "productName", value = "产品名称"),
            @ApiImplicitParam(name = "stockAttribute", value = "库存属性"),
            @ApiImplicitParam(name = "date", value = "开始时间"),
            @ApiImplicitParam(name = "sort", value = "排序方式"),
            @ApiImplicitParam(name = "sortColumn", value = "排序列"),
    })
    public void productSegAgeExport(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = this.getProductSegMap(request);
        String ids = request.getParameter("ids");
        if (ids != null) {
            map.put("ids", Arrays.asList(ids.split(",")));
        }
        service.productSegAgeExport(map, response);
    }




    @UserSessionRequired
    @GetMapping("/boxSegAge")
    @ApiOperation(value = "分段箱库龄（分页）", notes = "分段箱库龄（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storageIds", value = "仓库 多个,隔开"),
            @ApiImplicitParam(name = "sku", value = "箱类型号"),
            @ApiImplicitParam(name = "productName", value = "自定义箱条码"),
            @ApiImplicitParam(name = "inBoxSku", value = "SKU"),
            @ApiImplicitParam(name = "date", value = "开始时间"),
            @ApiImplicitParam(name = "sort", value = "排序方式"),
            @ApiImplicitParam(name = "sortColumn", value = "排序列"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = StockSkuSnapPageDto.class))
    public PageResult boxSegAge(HttpServletRequest request){

        Map<String, Object> map = this.getBoxSegMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.productSegAge(map);
    }

    public Map<String, Object> getBoxSegMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        String sku = request.getParameter("sku");
        String productName = request.getParameter("productName");
        String sort = request.getParameter("sort");
        String sortColumn = request.getParameter("sortColumn");
        String[] storageIds = request.getParameterValues("storageIds");
        map.put("sku", sku);
        map.put("productName", productName);
        map.put("sort", sort);
        map.put("sortColumn", sortColumn);
        if (storageIds != null && storageIds.length > 0) {
            map.put("storageIds", Arrays.asList(storageIds));
        }
        map.put("companyId", getCompanyId());
        map.put("tenantId", getTenantId());
        String customizeBarcode = request.getParameter("customizeBarcode");
        map.put("customizeBarcode", customizeBarcode);
        String inBoxSku = request.getParameter("inBoxSku");
        map.put("inBoxSku", inBoxSku);
        String date = request.getParameter("date");
        map.put("date", date);
        map.put("type", 2);
        return map;
    }


    @UserSessionRequired
    @GetMapping("/boxSegAgeExport")
    @ApiOperation(value = "分段箱库龄导出", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id 多个,隔开  sku___whId___stockAttribute___date 组成"),
            @ApiImplicitParam(name = "storageIds", value = "仓库 多个,隔开"),
            @ApiImplicitParam(name = "sku", value = "箱类型号"),
            @ApiImplicitParam(name = "productName", value = "自定义箱条码"),
            @ApiImplicitParam(name = "inBoxSku", value = "SKU"),
            @ApiImplicitParam(name = "date", value = "开始时间"),
            @ApiImplicitParam(name = "sort", value = "排序方式"),
            @ApiImplicitParam(name = "sortColumn", value = "排序列")
    })
    public void boxSegAgeExport(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = this.getBoxSegMap(request);
        String ids = request.getParameter("ids");
        if (ids != null) {
            map.put("ids", Arrays.asList(ids.split(",")));
        }
        service.boxSegAgeExport(map, response);
    }
}

